﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using DevExpress.Utils;
using DevExpress.XtraMap;

namespace DevExpress.MailClient.Win.Controls.eMap
{
    public partial class ucGeoMap : DevExpress.XtraEditors.XtraUserControl
    {
        SuperToolTip superToolTip;
        ToolTipTitleItem titleItem;
        ToolTipItem contentItem;

        ImageTilesLayer TilesLayer { get { return (ImageTilesLayer)(mapControl1.Layers[0]); } }


        public void CenterTo(double longit, double latit)
        {
            mapControl1.CenterPoint = new GeoPoint(latit, longit);
            mapControl1.ZoomLevel = 7;
        }

        public ucGeoMap()
        {
            InitializeComponent();
        }

        void InitializeSuperToolTip()
        {
            superToolTip = new SuperToolTip() { MaxWidth = 350 };
            titleItem = new ToolTipTitleItem();
            contentItem = new ToolTipItem();
            superToolTip.Items.Add(titleItem);
            superToolTip.Items.Add(contentItem);
        }


        void UpdateSuperToolTip(string content, MapItem item)
        {
            contentItem.Text = content;
            if (item != null)
            {
                Image im = Image.FromFile(item.Attributes["ImagePath"].Value.ToString());
                try
                {
                    im = Image.FromFile(item.Attributes["ImagePath"].Value.ToString());
                }
                catch
                {
                    //in case of anny issue
                    //im = Properties.Resources._default;
                }
                contentItem.Image = im;
                titleItem.Text = "Location Details";
            }
        }


        public void InitGeoMap(string bingKey, List<GeoLocation> source)
        {
            InitializeSuperToolTip();
            try
            {
                var imColl = new DevExpress.Utils.ImageCollection();
                imColl.ImageSize = new Size(16, 16);


                mapControl1.ZoomLevel = 7;
                var bingprov = new BingMapDataProvider { BingKey = bingKey };
                TilesLayer.DataProvider = bingprov;

                var listOfAttr = new List<MapItemAttributeMapping>
                                     {
                                         //new MapItemAttributeMapping {Member = "CategoryId", Name = "CategoryId"},
                                         new MapItemAttributeMapping {Member = "CatName", Name = "CatName"},
                                         new MapItemAttributeMapping {Member = "RegionName", Name = "RegionName"},
                                         new MapItemAttributeMapping {Member = "Latitude", Name = "Latitude"},
                                         new MapItemAttributeMapping {Member = "Longitude", Name = "Longitude"},
                                         new MapItemAttributeMapping {Member = "ImagePath", Name = "ImagePath"},
                                         new MapItemAttributeMapping {Member = "IconPath", Name = "IconPath"}
                                     };

                int i = 0;
                foreach (IGrouping<string, GeoLocation> geoLocations in source.GroupBy(n => n.IconPath))
                {
                    //var category_id = geoLocations[geoLocations.Key];
                    var icpath = geoLocations.Key;
                    Image im = Image.FromFile(icpath);
                    try
                    {
                        im = Image.FromFile(icpath);
                    }
                    catch
                    {
                        //if there is any issue
                        //im = Properties.Resources.pushpin;
                    }

                    imColl.AddImage(im);
                    var lyer = GetLayer(i);
                    lyer.AttributeMappings.AddRange(listOfAttr);
                    mapControl1.Layers.Add(lyer);

                    lyer.DataSource = source.Where(m => m.IconPath == icpath).Select(_geoLocation => new GeoLocation
                                                                                              {
                                                                                                  //CategoryId = _geoLocation.CategoryId,
                                                                                                  Longitude = _geoLocation.Longitude,
                                                                                                  IconPath = _geoLocation.IconPath,
                                                                                                  ImagePath = _geoLocation.ImagePath,
                                                                                                  Latitude = _geoLocation.Latitude,
                                                                                                  RegionName = _geoLocation.RegionName,
                                                                                                  CatName = _geoLocation.CatName,
                                                                                                  ImgInd = i
                                                                                              }).ToList();
                    i++;
                }
                mapControl1.ImageList = imColl;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        private DevExpress.XtraMap.VectorItemsLayer GetLayer(int imInd)
        {
            var vcl = new DevExpress.XtraMap.VectorItemsLayer { ItemImageIndex = imInd };
            vcl.Mappings.Latitude = "Latitude";
            vcl.Mappings.Longitude = "Longitude";
            vcl.ToolTipPattern =
                "Category:  {CatName}\r\n\r\nRegion:  {RegionName}\r\n\r\nLatitude:  {Latitude}\r\n\r\nLognitu" +
                "de: {Longitude}";
            return vcl;
        }


        private void toolTipController1_BeforeShow(object sender, ToolTipControllerShowEventArgs e)
        {
            UpdateSuperToolTip(e.ToolTip, e.SelectedObject as MapItem);
            e.AutoHide = false;
            e.SuperTip = superToolTip;
        }
    }

    public class GeoLocation
    {

        public int CategoryId { get; set; }

        public string CatName { get; set; }

        public string RegionName { get; set; }

        public double Latitude { get; set; }

        public double Longitude { get; set; }

        public string ImagePath { get; set; }

        public string IconPath { get; set; }

        public int ImgInd { get; set; }
    }
}
